# encoding=utf-8

import time
import xmlrpclib

from flask import Flask, render_template
from flask import request, redirect, Blueprint

from cloud_testing.utils import register_ip, query_ip, save_report,query_report


# 磁盘的功能放到这里
disk = Blueprint('disk', __name__,
                       url_prefix='/disk',
                       template_folder='templates',
                       static_folder='static')

@disk.route('/route/<name>', methods=['GET', 'POST'])
def disk_route(name):
    if name == 'virtio':
        return virtioblk_init()
    elif name == 'encrypt':
        return encrypt_init()
    elif name == 'erase':
        return erase_init()
    elif name == 'mount':
        return mount_init()
    else:
        return render_template('content.html', welcomePage=False)

# 初始化方式
def virtioblk_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据隔离', nav_b=u'虚拟磁盘驱动',list=query_ip(),test='/disk/virtio/test',report='/disk/virtio/report')

def encrypt_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据隔离', nav_b=u'磁盘加密检测',list=query_ip(),test='/disk/encrypt/test',report='/disk/encrypt/report')

def erase_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据隔离', nav_b=u'磁盘擦出检测',list=query_ip(),test='/disk/erase/test',report='/disk/erase/report')

def mount_init():
    introduce = {
        'content':u'写上自己需要的内容',
        'method':u'测试方法',
        'setup':u'设置：centos7，wireshark...',
        'testmethod':u'测试方式,点击按钮即可',
        'report':u'报告',
        'picture':'/static/img/openstack.jpg'
        }
    return render_template('content.html', introduce=introduce, welcomePage=False, nav_a=u'数据隔离', nav_b=u'磁盘独占检测',test='/disk/virtio/test',report='/disk/virtio/report')
# 虚拟磁盘驱动数据安全检测
@disk.route('/virtio/test', methods=['GET', 'POST'])
def virtioblk_test():
    # virtio blk测试按钮事件
    import os,time
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        keyword = request.form['args']
        host = "http://" + ip + ':' + port
        proxy = xmlrpclib.ServerProxy(host)
        result=proxy.vdisk({'keyword':keyword})
        if result=='error':
            return 'error'
        # print (result.data)#存入report表
        save_report({"ip":ip, "port":port, "server_name":"virtio_blk", "result":(result.data)})
        return 'success'
    else:
        return 'error'
    
    return "success"
@disk.route('/virtio/report', methods=['GET', 'POST'])
def virtioblk_report():
    # virtio blk报告事件
    return render_template('content.html', welcomePage=False)

# 磁盘加密检测
@disk.route('/encrypt/test', methods=['GET', 'POST'])
def diskencrypt_test():
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        diskpath = request.form['args']
        host = "http://" + ip + ':' + port
        proxy = xmlrpclib.ServerProxy(host)
        result=proxy.disk_encryption({'diskpath':diskpath})
        if result=='error':
            return 'error'
        # print (result.data)#存入report表
        save_report({"ip":ip, "port":port, "server_name":"disk_encrypt", "result":(result.data)})
        return 'success'
    else:
        return 'error'
    #return "success"

@disk.route('/encrypt/report', methods=['GET', 'POST'])
def diskencrypt_report():
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        return str(query_report(ip, port, 'disk_encrypt'))
    return 'error'

# 磁盘擦出检测
@disk.route('/erase/test', methods=['GET', 'POST'])
def diskerase_test():
    if request.method == 'POST':
        ip = request.form['ip']
        port = request.form['port']
        diskpath = request.form['args']
        host = "http://" + ip + ':' + port
        proxy = xmlrpclib.ServerProxy(host)
        result=proxy.wipe({'diskpath':diskpath})
        if result=='error':
            return 'error'
        # print (result.data)#存入report表
        save_report({"ip":ip, "port":port, "server_name":"disk_wipe", "result":(result.data)})
        return 'success'
    else:
        return 'error'

@disk.route('/erase/report', methods=['GET', 'POST'])
def diskerase_report():
    return render_template('content.html', welcomePage=False)
